package com.stox.ui.javafx.core.view;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import jfxtras.scene.control.window.CloseIcon;
import jfxtras.scene.control.window.Window;

import com.stox.ui.javafx.core.animation.Transitions;
import com.stox.ui.javafx.core.widget.StoxStage;

public class View extends Window {

	public View(String title){
		super(title);
	}
	
	public View() {
		setId("iwindow");
		CloseIcon closeIcon = new CloseIcon(this);
		setCloseTransition(Transitions.getClose(this));
		getRightIcons().add(closeIcon);
		closeIcon.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent arg0) {
				close();
			}
		});
		
	//	boundsInParentProperty().addListener(new BoundsInParentPropertyChangeListener(this));
	//	boundsInLocalProperty().addListener(new BoundsInLocalPropertyChangeListener(this));
	}
	
	public void open() {
		final double minHeight = getMinHeight();
		final double minWidth = getMinWidth();
		final double maxHeight = getMaxHeight();
		final double maxWidth = getMaxWidth();
		
		setMinHeight(getPrefHeight());
		setMinWidth(getPrefWidth());
		setMaxHeight(getPrefHeight());
		setMaxWidth(getPrefWidth());
		
		if(!StoxStage.getInstance().getRootPane().getChildren().contains(this))
			StoxStage.getInstance().getRootPane().getChildren().add(this);
		
		transitionOpen();
		autosize();
		
		setMinHeight(minHeight);
		setMinWidth(minWidth);
		setMaxHeight(maxHeight);
		setMaxWidth(maxWidth);
	}
	
	public void transitionOpen(){
		Transitions.getOpen(View.this).play();
	}
	
}
